package com.nari.ies.server.service.impl;
import com.nari.ies.server.entity.AreaAndPlantDO;
import com.nari.ies.server.entity.AreaNameNum;
import com.nari.ies.server.entity.PlantType;
import com.nari.ies.server.mapper.AreaAndPlantMapper;
import com.nari.ies.server.service.IAreaAndPlantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("areaAndPlantService")
public class AreaAndPlantServiceImpl implements IAreaAndPlantService {

    @Autowired
    private AreaAndPlantMapper areaAndPlantMapper;

    @Override
    public List<Map<String, Object>> queryNeAreaNameAndPlantName() {
        //查询区域ID和名称
        List<Map<String, Object>> dataList = areaAndPlantMapper.queryNeAreaNameAndPlantName();

        return dataList;
    }

    @Override
    public List<AreaAndPlantDO> createTreeDataStructure(List<Map<String, Object>> maps) {

        List<AreaAndPlantDO> resultList = initAreaAndPlantDOList();

        for (Map<String, Object> map : maps) {
            String provinceId = map.get("ID").toString();
            if(provinceId == null || !AreaNameNum.isProvincial(provinceId.toString())){
                continue;
            }
            AreaAndPlantDO provinceDO = isExistence(resultList, provinceId.toString());
            List<Map<String, Object>> sbuMaps = areaAndPlantMapper.querySubById(provinceId);
            log.info("省级下的变电站ID为"+sbuMaps);
            if (sbuMaps!=null&&sbuMaps.size()>0) {
                for (Map<String, Object> submap : sbuMaps) {
                    String subId = submap.get("BWDID").toString();
                    if(subId!=null){
                        Map<String, Object> names = areaAndPlantMapper.querySubNameById(subId);
                        log.info("对应的变电站名称为"+names);
                        if(names!=null){
                            String subName = names.get("NAME").toString();
                            AreaAndPlantDO subDO = new AreaAndPlantDO();
                            subDO.setType("BDZ");
                            subDO.setId(subId);
                            subDO.setTitle(subName);
                            subDO.setChildren(new ArrayList<AreaAndPlantDO>());
                            log.info("变电站信息为"+subDO.toString());
                            provinceDO.getChildren().add(subDO);
                            log.info("省级下拉框显示："+subDO.toString());
                        }
                    }
                }
            }
        }
        log.info("左侧树为:"+resultList);
        return resultList;

    }
    @Override
    public List<Map<String, Object>> queryPlantInfoByTypeAndId(String type, String bdzId,String areaId) {
        String tableName = "";
        if (PlantType.isWind(type)){
            tableName="NUSP.rt_con_plant_wind s ";
        }else {
            tableName="NUSP.rt_con_plant_pv s ";
        }
        List<Map<String, Object>> dataList = areaAndPlantMapper.queryPlantInfoByTypeAndId(tableName,bdzId,areaId);
        return dataList;
    }

    @Override
    public List<Map<String, Object>> queryPlantInfoByWFPId(String type, String areaId) {
        String tableName = "";
        if (PlantType.isWind(type)){
            tableName="NUSP.rt_con_plant_wind s ";
        }else {
            tableName="NUSP.rt_con_plant_pv s ";
        }
        List<Map<String, Object>> dataList = areaAndPlantMapper.queryPlantInfoByWFPId(tableName,areaId);
        return dataList;
    }

    private List<AreaAndPlantDO> initAreaAndPlantDOList() {
        List<AreaAndPlantDO> resultList = new ArrayList<>();

        AreaAndPlantDO bwdDO = new AreaAndPlantDO();
        bwdDO.setType("BDZ");
        bwdDO.setId(null);
        bwdDO.setChildren(new ArrayList<AreaAndPlantDO>());
        bwdDO.setTitle("未分配");

        AreaAndPlantDO ZDDO = new AreaAndPlantDO();
        ZDDO.setType("north-west");
        ZDDO.setId(AreaNameNum.XI_BEI_ZHONG_DIAO);
        ZDDO.setChildren(new ArrayList<AreaAndPlantDO>());
        ZDDO.setTitle("西北");
        resultList.add(ZDDO);

        AreaAndPlantDO shanXiDO = new AreaAndPlantDO();
        shanXiDO.setType("province");
        shanXiDO.setId(AreaNameNum.SHAN_XI);
        shanXiDO.setTitle("陕西");
        shanXiDO.setChildren(new ArrayList<AreaAndPlantDO>());
        shanXiDO.getChildren().add(bwdDO);
        ZDDO.getChildren().add(shanXiDO);

        AreaAndPlantDO ganSuDO = new AreaAndPlantDO();
        ganSuDO.setType("province");
        ganSuDO.setId(AreaNameNum.GAN_SU);
        ganSuDO.setTitle("甘肃");
        ganSuDO.setChildren(new ArrayList<AreaAndPlantDO>());
        ganSuDO.getChildren().add(bwdDO);
        ZDDO.getChildren().add(ganSuDO);

        AreaAndPlantDO ningXiaDO = new AreaAndPlantDO();
        ningXiaDO.setType("province");
        ningXiaDO.setId(AreaNameNum.NING_XIA);
        ningXiaDO.setTitle("宁夏");
        ningXiaDO.setChildren(new ArrayList<AreaAndPlantDO>());
        ningXiaDO.getChildren().add(bwdDO);
        ZDDO.getChildren().add(ningXiaDO);

        AreaAndPlantDO qingHaiDO = new AreaAndPlantDO();
        qingHaiDO.setType("province");
        qingHaiDO.setId(AreaNameNum.QING_HAI);
        qingHaiDO.setTitle("青海");
        qingHaiDO.setChildren(new ArrayList<AreaAndPlantDO>());
        qingHaiDO.getChildren().add(bwdDO);
        ZDDO.getChildren().add(qingHaiDO);

        AreaAndPlantDO xinJiangDO = new AreaAndPlantDO();
        xinJiangDO.setType("province");
        xinJiangDO.setId(AreaNameNum.XIN_JIANG);
        xinJiangDO.setTitle("新疆");
        xinJiangDO.setChildren(new ArrayList<AreaAndPlantDO>());
        xinJiangDO.getChildren().add(bwdDO);
        ZDDO.getChildren().add(xinJiangDO);

        AreaAndPlantDO xibeizhidiaoDO = new AreaAndPlantDO();
        xibeizhidiaoDO.setType("province");
        xibeizhidiaoDO.setId(AreaNameNum.XI_BEI_ZD);
        xibeizhidiaoDO.setTitle("西北直调");
        xibeizhidiaoDO.setChildren(new ArrayList<AreaAndPlantDO>());
        xibeizhidiaoDO.getChildren().add(bwdDO);
        ZDDO.getChildren().add(xibeizhidiaoDO);

        return resultList;
    }

    /**
     * 返回对应ID的DO
     * @param resultList
     * @param id
     * @return
     */
    private AreaAndPlantDO isExistence(List<AreaAndPlantDO> resultList, String id) {
        for (AreaAndPlantDO areaAndPlantDO : resultList) {
            if (id.equals(areaAndPlantDO.getId())) {
                return areaAndPlantDO;
            }
            List<AreaAndPlantDO> plantList = areaAndPlantDO.getChildren();
            if (plantList == null || plantList.size()==0){
                continue;
            }

            for (AreaAndPlantDO plantDO : plantList) {
                if (id.equals(plantDO.getId())) {
                    return plantDO;
                }
            }
        }

        return null;
    }
}
